import { Message } from 'element-ui'
import request from '../request'
const baseUrl = 'https://api.huobi.de.com'
const ticker = `${baseUrl}/market/detail/merged`
const tickers = `${baseUrl}/market/tickers`
const baseContractUrl = 'https://api.hbdm.vn' // 币本位，api.hbdm.vn域名使用的是AWS的CDN服务，理论上AWS服务器用户使用此域名会更快更稳定；api.hbdm.com域名使用的是Cloudflare的CDN服务。
const marketsUSD = `${baseContractUrl}/swap-ex/market/detail/batch_merged`
const marketsUSDT = `${baseContractUrl}/linear-swap-ex/market/detail/batch_merged`

export async function getTicket(symbol) {
  const { status, tick } = await request({
    url: `${ticker}?symbol=${symbol}`,
    method: 'get'
  })
  if (status !== 'ok') {
    Message({
      message: '没有对应的交易币种',
      type: 'error',
      duration: 3 * 1000
    })
  }
  return tick
}

export async function getTickets() {
  const { status, data } = await request({
    url: tickers,
    method: 'get'
  })
  if (status !== 'ok') {
    Message({
      message: '数据出错，请重试',
      type: 'error',
      duration: 3 * 1000
    })
  }
  return data
}

/**
 * 获取币合约聚合数据
 * @returns []
 */
export async function getMarketUSD() {
  const { status, ticks } = await request({
    url: marketsUSD,
    method: 'get'
  })
  if (status !== 'ok') {
    Message({
      message: '数据出错，请重试',
      type: 'error',
      duration: 3 * 1000
    })
  }
  return ticks
}

/**
 * 获取U本位合约聚合数据
 * @returns []
 */
export async function getMarketUSDT() {
  const { status, ticks } = await request({
    url: marketsUSDT,
    method: 'get'
  })
  if (status !== 'ok') {
    Message({
      message: '数据出错，请重试',
      type: 'error',
      duration: 3 * 1000
    })
  }
  return ticks
}

